<template>
	<rui-pages go="back">
		<rui-card title="用户注册">
			<view class="register">
				<view class="register_form">
					<view class="register_form_item">
						<view class="register_form_item_label">
							用户权限
						</view>
						<view class="register_form_item_input">
							<radio-group @change="roleChange">
								<label class="role">
									<view v-for="(item, index) in setting.role" :key="index">
										<view class="role_item">
											<radio :value="item.id" :checked="form.role_id == item.id" />{{item.name}}
										</view>
									</view>
								</label>
							</radio-group>
						</view>
					</view>
					<view class="register_form_item">
						<view class="register_form_item_label">
							昵称
						</view>
						<view class="register_form_item_input">
							<input type="text" placeholder="请输入昵称" v-model="form.nickname">
						</view>
					</view>
					<view class="register_form_item">
						<view class="register_form_item_label">
							用户名
						</view>
						<view class="register_form_item_input">
							<input type="text" placeholder="请输入用户名" v-model="form.username">
						</view>
					</view>
					<view class="register_form_item">
						<view class="register_form_item_label">
							密码
						</view>
						<view class="register_form_item_input">
							<input type="password" placeholder="请输入密码" v-model="form.password">
						</view>
					</view>
					<view class="register_form_item">
						<view class="register_form_item_label">
							确认密码
						</view>
						<view class="register_form_item_input">
							<input type="password" placeholder="请输入密码" v-model="form.confirm">
						</view>
					</view>
				</view>
				<view class="register_btn">
					<button class="register_submit" @click="submit">注册</button>
				</view>
			</view>
		</rui-card>
	</rui-pages>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import UserModel from '@/db/UsersModel';

	import {
		settingStore
	} from '@/store';
	let setting = settingStore();

	let form = ref({
		nickname: "",
		username: "",
		password: "",
		confirm: "",
		role_id: 2
	})

	const roleChange = (e) => {
		form.value.role_id = e.detail.value
	}

	let users = new UserModel();

	const submit = () => {
		if (!form.value.username || !form.value.password) {
			uni.showToast({
				title: "用户名密码不能为空",
				icon: "none"
			})
			return;
		}
		if (form.value.username.length < 3) {
			uni.showToast({
				title: "用户名不能小于4位",
				icon: "none"
			})
			return;
		}
		if (form.value.password.length < 6) {
			uni.showToast({
				title: "密码不能小于6位",
				icon: "none"
			})
			return;
		}
		if (!form.value.confirm || form.value.password !== form.value.confirm) {
			uni.showToast({
				title: "两次密码不一致",
				icon: "none"
			})
			return;
		}
		let data = {
			...form.value
		}
		delete(data.confirm)

		data.password = uni.$rui.md5(data.password)
		data.created_at = uni.$rui.date.formatDate();

		
		users.create(data).then(res => {
			uni.showToast({
				title: "添加成功"
			})
			setTimeout(_ => {
				uni.navigateBack({
					delta: 1
				})
			}, 1500)
		})
	}
</script>


<style lang="scss" scoped>
	.role {
		display: flex;
		align-items: center;

		&_item {
			margin-right: 10px;
			display: flex;
			align-items: center;
		}
	}

	.register {
		height: 400px;
		width: 400px;
		margin: 60px auto 0;

		&_btn {
			margin-top: 40px;
		}

		&_submit {
			background: var(--color);
			color: #fff;
			width: inherit;
		}

		&_form {
			&_item {
				display: flex;
				align-items: center;
				margin: 15px 0;

				&_label {
					width: 80px;
					text-align: right;
					margin-right: 10px;

					&::after {
						content: ":";
					}
				}

				&_input {
					background: var(--bg);
					padding: 10px;
					border-radius: 5px;
					width: 300px;

					input {
						width: 100%;
					}
				}
			}
		}
	}
</style>